package sql

import (
	"time"

	"gorm.io/gorm"
)

// User 用户表（注意字段与 created_at/updated_at 的映射）
type User struct {
	gorm.Model
	Username string `gorm:"type:varchar(50);uniqueIndex;not null"`
	Password string `gorm:"type:varchar(255);not null"`
	Name     string `gorm:"type:varchar(50);not null"`
	Role     string `gorm:"type:enum('admin','teacher','student');not null"`
}

// Event 赛事表（时间字段自动映射）
type Event struct {
	gorm.Model
	Name      string    `gorm:"type:varchar(100);not null"`
	StartTime time.Time `gorm:"column:start_time;not null"`
	EndTime   time.Time `gorm:"column:end_time;not null"`
	Rules     string    `gorm:"type:text"`
}

// Registration 报名表（处理 SQL 关键字冲突）
type Registration struct {
	gorm.Model
	EventID          uint      `gorm:"column:event_id;not null"`
	Name             string    `gorm:"type:varchar(50);not null"`
	Age              int       `gorm:"not null"`
	Gender           string    `gorm:"type:enum('male','female');not null"`
	Group            string    `gorm:"type:varchar(50);column:group_name;not null"` // 重命名 group 字段
	RegistrationTime time.Time `gorm:"default:CURRENT_TIMESTAMP"`
}

// Result 成绩表（外键关联配置）
type Result struct {
    gorm.Model
    EventID         uint         `gorm:"not null;index"`
    RegistrationID  uint         `gorm:"not null;index"`
    Score          string       `gorm:"type:varchar(50);not null"`
    Event          Event        `gorm:"foreignKey:EventID;references:ID;constraint:OnDelete:CASCADE"`
    Registration   Registration `gorm:"foreignKey:RegistrationID;references:ID;constraint:OnDelete:CASCADE"`
}
